﻿import QtQuick 2.0

import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4


import GpioDataitem 1.0
import SerialDataitem 1.0
import PrintLogitem 1.0

import ConfigFileitem 1.0

Rectangle {
    id:exhaleWait
    color: "#F2F2F2"


    signal sigCancelClick();
    signal sigSuccessClick();


    property double  mistakeNumbers:  configFileitem.getCoCheck() //0.01
    property int  mistakeTime:configFileitem.getCoTakeTime() //20
    property int  writeNumbers:0
    property double  writeAllNumbers:0.0

    property double  runSpaces:1.0/(maxConfigTime + currentCoTakes+ 10)


    property int  spaceNumbers:0
    property int  spaceAllNumbers:configFileitem.getCoSpace() // 5
    property int  currentCoTakes:configFileitem.getCoTake()

    property double currentPpm: 0
    property double currentPpms: 0

    property int currentNums: 0

    property double currentWriteNumbers: 0.0

    property int maxConfigTime:configFileitem.getCoTime()



    property bool iniNumbesInfrs:false

    function handlCancelClick(){
        exhaleTimer.stop();
        if(gpioDataitem.getD1() == 1){
            gpioDataitem.closeD1();
        }
        if(gpioDataitem.getD2() == 1){
            gpioDataitem.closeD2();
        }
        if(gpioDataitem.getD3() == 1){
            gpioDataitem.closeD3();
        }
        if(gpioDataitem.getD4() == 1){
            gpioDataitem.closeD4();
        }
        exhaleWait.iniNumbesInfrs = false
        gpioDataitem.closeCo();
        exhaleWait.sigCancelClick();
        printLogitem.printInfor("qml:exhaleWait sigCancelClick");
    }

    MouseArea{
        anchors.fill: parent
        onPressed: {
        }
        onReleased: {
        }
        onClicked: {
        }
    }
    function openFunction(){
        gpioDataitem.openCo();
        console.log("exhaleWait openFunction ")
        exhaleWait.iniNumbesInfrs = false;
        currentPpm= -1
        currentPpms= -1
        exhalePercent.value = 0.0;
        currentWriteNumbers = 0.0;
        currentNums = 0;
        writeNumbers = 0;
        writeAllNumbers = 0.0;

        if(gpioDataitem.getD1() == 1){
            gpioDataitem.closeD1();
        }
        if(gpioDataitem.getD3 () == 0){
            gpioDataitem.openD3();
        }
        if(gpioDataitem.getD2() == 1){
            gpioDataitem.closeD2();
        }
        if(gpioDataitem.getD4() == 1){
            gpioDataitem.closeD4();
        }

        printLogitem.printInfor("qml:exhaleWait openFunction");

        exhaleTimer.start();
        handCurrentDate();
    }

    function closeFunction(){
        console.log("exhaleWait closeFunction ")
        exhaleWait.iniNumbesInfrs = false;
        currentNums = 0;
        exhalePercent.value = 0.0;
        currentWriteNumbers = 0.0;
        exhaleTimer.stop();
        printLogitem.printInfor("qml:exhaleWait closeFunction");

    }
    onVisibleChanged: {

        if(visible){

        }else{

        }
    }



    function handCurrentDate(){
        var currentDate = new Date();
        var date_tring = currentDate.toLocaleDateString();
        //  console.log("local = ",date_tring,currentDate.getFullYear(),currentDate.getDay())
        var curyear = parseInt(currentDate.getFullYear().toString());
        var curmonth = parseInt((currentDate.getMonth()+1).toString())
        var curday = parseInt(currentDate.getDate().toString())


        var curhours= parseInt(currentDate.getHours());
        var curminute= parseInt(currentDate.getMinutes());
        var cursec= parseInt(currentDate.getSeconds());

        var curhourstr="";
        if(curhours < 10){
            curhourstr="0"+curhours;
        }else{
            curhourstr=""+curhours;
        }
        var curminutestr="";
        if(curminute < 10){
            curminutestr="0"+curminute;
        }else{
            curminutestr=""+curminute;
        }

        var cursecstr="";
        if(cursec < 10){
            cursecstr="0"+cursec;
        }else{
            cursecstr=""+cursec;
        }

        dateRightText.text = curyear+"年"+curmonth+"月"+curday+"日" ;
        dateText.text = curhourstr+":"+curminutestr+":"+cursecstr;
    }

    //顶部背景
    Rectangle{
        id:topBackground

        anchors.left: parent.left
        anchors.top: parent.top
        width: parent.width
        height: 276
        color: "#FFFFFF"
        Image {
            width: parent.width
            height: parent.height
            anchors.left: parent.left
            anchors.top: parent.top
            source: "qrc:/images/topbackgroundimage.png"
        }
        Text {
            id: topMacheneName
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 82
            width: parent.width
            height: 36
            color: "#FFFFFF"
            font.bold: true
            font.italic: true
            font.pixelSize: 33
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            text: qsTr("呼气分析仪")
        }
        Image {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 67
            anchors.leftMargin: 59
            width: 48
            height: 48
            source: "qrc:/images/personimages.png"
        }
        Image {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 72
            anchors.leftMargin: 64
            width: 38
            height: 38
            source: "qrc:/images/personinimages.png"
        }

        Text {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 67
            anchors.leftMargin: 110
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            width: 150
            height: 48
            color: "#FFFFFF"
            font.pixelSize: 22
            text: qsTr("Welcome")
        }

        Text {
            id: dateText
            anchors.left:  parent.left

            anchors.top: parent.top
            anchors.topMargin: 20
            height: 20
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            width: parent.width

            color: "#FFFFFF"
            font.pixelSize: 16
            text: qsTr("")
        }

        Text {
            id: dateRightText
            anchors.right:  parent.right
            anchors.rightMargin: 170
            anchors.top: parent.top
            anchors.topMargin: 20
            height: 20
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignRight
            width: 200
            color: "#FFFFFF"
            font.pixelSize: 16
            text: qsTr("")
        }

        Text {
            id: logText
            anchors.left:  parent.left
            anchors.leftMargin: 51
            anchors.top: parent.top
            anchors.topMargin: 116
            height: 25
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            width: logText.contentWidth
            color: "#FFFFFF"
            font.pixelSize: 20
            font.bold: true
            font.italic: true
            text:  qsTr("KAMYRON")+qsTr("  ")+qsTr("卡米龙")
        }

        Timer {
            id: dateTimer
            interval: 1000;
            running: true;
            repeat: true
            onTriggered:{
                handCurrentDate();
            }
        }

    }


    //中间背景
    Rectangle{
        id:midBackground
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.leftMargin: 30
        anchors.topMargin: 176
        anchors.rightMargin: 30
        anchors.bottomMargin: 130
        color: "#FFFFFF"
        radius: 25

        Rectangle{
            id:midInforBackground
            //            width: 400
            //            height: 300
            //            anchors.right:  parent.right
            //            anchors.verticalCenter:parent.verticalCenter
            //            anchors.rightMargin: 60
            //            radius: 10
            //            color: "#F2F2F2"
            width: parent.width
            height: parent.height
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.leftMargin: 5
            anchors.topMargin: 5

            radius: 10
            color: "#00000000"

            Text {
                id: coTipText
                anchors.left:  parent.left

                anchors.top: parent.top
                anchors.topMargin: 30
                height: 50
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                width: midInforBackground.width
                color: "#E8C06B"
                font.pixelSize: 30
                font.bold: true
                text: qsTr("呼出气等待测量")
            }




        }


        //        ClockDial{
        //            id:clockDial
        //            anchors.left:  parent.left
        //            anchors.leftMargin: 30
        //            anchors.verticalCenter:parent.verticalCenter
        //            //  visible: exhaleWait.visible

        //        }
    }

    ProgressBar {
        id: exhalePercent
        value: 0.0
        // padding: 2
        anchors.top:  midBackground.bottom
        anchors.topMargin: 10
        anchors.left:  midBackground.left
        anchors.right: midBackground.right

        height: 20

        style: ProgressBarStyle {
            background: Rectangle {
                radius: 10
                color: "#e6e6e6"
                border.color: "#00000000"
                border.width: 0
                implicitWidth: 200
                implicitHeight: 20
            }
            progress: Rectangle {
                color: "#24B9B7"
                border.color:  "#00000000"
                radius: 10
            }
        }


    }


    Rectangle{
        id:cancelBtn
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 39
        width: 250
        height: 50
        radius: 25
        color:( cancelBtn.cancelBtnPressed ? "#5C24B9B7":"#FF24B9B7")

        property bool cancelBtnPressed: false
        Text {
            width: parent.width
            height: parent.height
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            font.pixelSize: 30
            color: "#FFFFFF"
            text: qsTr("取消")
        }

        MouseArea{
            anchors.fill: parent

            onPressed: {
                cancelBtn.cancelBtnPressed = true
            }
            onReleased: {
                cancelBtn.cancelBtnPressed = false
            }
            onClicked: {
                exhaleTimer.stop();
                if(gpioDataitem.getD1() == 1){
                    gpioDataitem.closeD1();
                }
                if(gpioDataitem.getD2() == 1){
                    gpioDataitem.closeD2();
                }
                if(gpioDataitem.getD3() == 1){
                    gpioDataitem.closeD3();
                }
                if(gpioDataitem.getD4() == 1){
                    gpioDataitem.closeD4();
                }
                exhaleWait.iniNumbesInfrs = false
                gpioDataitem.closeCo();
                exhaleWait.sigCancelClick();
                printLogitem.printInfor("qml:exhaleWait sigCancelClick");

            }
        }

    }



    Timer{
        id:exhaleTimer
        repeat: false
        running: false
        interval: 1000
        onTriggered: {
            exhaleTimer.stop()
            if(!exhaleWait.visible){
                return;
            }
            currentNums++;
            console.log("exhaleWait.exhaleTimer indexNumbers=="+currentNums)
            currentWriteNumbers += runSpaces;
            exhalePercent.value += runSpaces ;//currentWriteNumbers / 100;
            if(currentNums == 3){

                if(gpioDataitem.getD1() == 0){
                    gpioDataitem.openD1();
                }
                if(gpioDataitem.getD3 () == 0){
                    gpioDataitem.openD3();
                }
                if(gpioDataitem.getD2() == 1){
                    gpioDataitem.closeD2();
                }
                if(gpioDataitem.getD4() == 1){
                    gpioDataitem.closeD4();
                }
                printLogitem.printInfor("qml:exhaleWait currentNums=="+currentNums);
            }

            if(currentNums == 6){

                if(gpioDataitem.getD1() == 0){
                    gpioDataitem.openD1();
                }
                if(gpioDataitem.getD3 () == 1){
                    gpioDataitem.closeD3();
                }
                if(gpioDataitem.getD2() == 1){
                    gpioDataitem.closeD2();
                }
                if(gpioDataitem.getD4() == 0){
                    gpioDataitem.openD4();
                }
                printLogitem.printInfor("qml:exhaleWait currentNums=="+currentNums);

            }

            //   if( parseInt( currentNums / 6 )  > 0 && parseInt( currentNums % 6  ) == 0  && currentNums <= 30   && currentNums >= 8 ){
            //            if( currentNums  >= 8 &&  currentNums <= 220    ){

            //                exhaleWait.iniNumbesInfrs = true
            //                printLogitem.printInfor("qml:exhaleWait  currentNums="+currentNums);
            //                // serialDataitem.readCoPotency();
            //            }
            if( currentNums  == 8    ){
                spaceNumbers =0;
                exhaleWait.iniNumbesInfrs = true
                printLogitem.printInfor("qml:exhaleWait  currentNums="+currentNums);
                // serialDataitem.readCoPotency();
            }
            if( currentNums  >= 8    ){
                serialDataitem.readCoPotency();
            }

            //            if(currentNums == 36){

            //                exhaleWait.iniNumbesInfrs = true;
            //                printLogitem.printInfor("qml:exhaleWait currentNums=="+currentNums);

            //                serialDataitem.readCoPotency();

            //            }



            // if(currentNums <= 230){

            exhaleTimer.start();
            //   }else{
            //                exhaleWait.iniNumbesInfrs = false;
            //                if(gpioDataitem.getD1() == 1){
            //                    gpioDataitem.closeD1();
            //                }
            //                if(gpioDataitem.getD2() == 1){
            //                    gpioDataitem.closeD2();
            //                }
            //                if(gpioDataitem.getD3() == 1){
            //                    gpioDataitem.closeD3();
            //                }
            //                if(gpioDataitem.getD4() == 1){
            //                    gpioDataitem.closeD4();
            //                }
            //                printLogitem.printInfor("qml:exhaleWait currentNums=="+currentNums);
            //                gpioDataitem.closeCo();
            //                exhaleWait.sigCancelClick();
            //          }
        }
    }



    GpioDataitem{
        id:gpioDataitem
    }

    SerialDataitem{
        id:serialDataitem

        onSigSendPotency: {

            if(exhaleWait.iniNumbesInfrs){

                //  exhaleWait.iniNumbesInfrs = false;
                if( currentNums < (currentCoTakes + 8)){
                    return;
                }
                if(currentPpm < potency ){
                    currentPpm = potency;
                }
                //                if(currentPpm < 0 ){
                //                    currentPpm = potency;
                //                }
                spaceNumbers++;
                if(spaceNumbers <= spaceAllNumbers){

                    return;
                }else{
                    spaceNumbers=0;
                }

                if(currentPpms >= 0 ){
                    if(Math.abs(currentPpms - potency) > mistakeNumbers){

                        writeNumbers = 0;
                        writeAllNumbers = 0.0;

                    }else{
                        writeAllNumbers+= potency;
                        writeNumbers+= 1;
                    }
                }

                currentPpms = potency;

                printLogitem.printInfor("qml:exhaleWait sigSendPotency currentPpm=="+currentPpm);
                if(currentNums >= (maxConfigTime + currentCoTakes + 8)){
                    //                    if(writeNumbers>0){

                    //                        currentPpm  =  writeAllNumbers / writeNumbers;
                    //                    }

                    exhaleTimer.stop();
                    exhaleWait.iniNumbesInfrs = false;
                    if(gpioDataitem.getD1() == 1){
                        gpioDataitem.closeD1();
                    }
                    if(gpioDataitem.getD2() == 1){
                        gpioDataitem.closeD2();
                    }
                    if(gpioDataitem.getD3() == 1){
                        gpioDataitem.closeD3();
                    }
                    if(gpioDataitem.getD4() == 1){
                        gpioDataitem.closeD4();
                    }
                    console.log("exhaleBlow.currentPpm ="+currentPpm)
                    gpioDataitem.closeCo();
                    exhaleWait.sigSuccessClick();
                }else{

                    if(writeNumbers > mistakeTime){
                        //                        if(writeNumbers>0){
                        //                            currentPpm  =  writeAllNumbers / writeNumbers;
                        //                        }
                        exhaleTimer.stop();
                        if(gpioDataitem.getD1() == 1){
                            gpioDataitem.closeD1();
                        }
                        if(gpioDataitem.getD2() == 1){
                            gpioDataitem.closeD2();
                        }
                        if(gpioDataitem.getD3() == 1){
                            gpioDataitem.closeD3();
                        }
                        if(gpioDataitem.getD4() == 1){
                            gpioDataitem.closeD4();
                        }
                        console.log("exhaleBlow.currentPpm ="+currentPpm)
                        gpioDataitem.closeCo();
                        exhaleWait.sigSuccessClick();
                        exhaleWait.iniNumbesInfrs = false;
                    }

                }

            }

        }
    }


    PrintLogitem{
        id:printLogitem
    }

    ConfigFileitem{
        id:configFileitem
    }

}
